package AddonsSheepContoller.Utils;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

import java.sql.*;

public class SQLiteManager {
    private Connection databaseConnection;
    private final Plugin resourcePlugin;

    public SQLiteManager(Plugin resourcePlugin) { this.resourcePlugin = resourcePlugin; }

    public boolean connectDatabase(String databaseAddress) {
        try {
            databaseConnection = DriverManager.getConnection(resourcePlugin.getDataFolder().toString()+"/"+databaseAddress);
            return true;
        }

        catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean insertData(String table, String[] insertColumns, String[] insertData, boolean[] stringType) {
        StringBuilder columnsStringBuilder = new StringBuilder();
        for(String format : insertColumns) columnsStringBuilder.append(format).append(", ");
        columnsStringBuilder.delete(columnsStringBuilder.length() - 2, columnsStringBuilder.length());

        StringBuilder dataStringBuilder = new StringBuilder();
        for(int i = 0; i < insertData.length; i++) {
            if(stringType[i]) dataStringBuilder.append("\"").append(insertData[i]).append("\", ");
            else dataStringBuilder.append(insertColumns[i]).append(", ");
        }
        dataStringBuilder.delete(dataStringBuilder.length() - 2, dataStringBuilder.length());
        String insertSQLCommand = String.format("INSERT INTO %s(%s) VALUES(%s)", table, columnsStringBuilder.toString(), dataStringBuilder.toString());

        try {
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > Ready process insert request!");
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > SQLite command: "+insertSQLCommand);
            PreparedStatement preparedStatement = databaseConnection.prepareStatement(insertSQLCommand);
            preparedStatement.close();
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > Request all are set!");
            return true;
        }

        catch(SQLException e)
        { e.printStackTrace(); return false; }
    }

    public ResultSet selectData(String table, String[] selectColumns) {
        StringBuilder columnsStringBuilder = new StringBuilder();
        for(String format : selectColumns) columnsStringBuilder.append(format).append(", ");
        columnsStringBuilder.delete(columnsStringBuilder.length() - 2, columnsStringBuilder.length());
        String selectSQLCommand = String.format("SELECT %s FROM %s", table, columnsStringBuilder.toString());

        try {
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > Ready process select request!");
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > SQLite command: "+selectSQLCommand);
            return databaseConnection.createStatement().executeQuery(selectSQLCommand);
        }

        catch(SQLException e)
        { e.printStackTrace(); return null; }
    }

    public boolean updateData(String table, String where, String values) {
        String updateSQLCommand = String.format("UPDATE %s SET %s WHERE %s", table, where, values);

        try {
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > Ready process update request!");
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > SQLite command: "+updateSQLCommand);
            databaseConnection.createStatement().executeUpdate(updateSQLCommand);
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > Request all are set!");
            return true;
        }

        catch(SQLException e)
        { e.printStackTrace(); return false; }
    }

    public boolean deleteData(String table, String where) {
        String deleteSQLCommand = String.format("DELETE FROM %s WHERE %s", table, where);

        try {
            Bukkit.getLogger().info("AddonsSheep-Controller(SQLite) > Ready process delete request!");
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > SQLite command: "+deleteSQLCommand);
            databaseConnection.createStatement().executeUpdate(deleteSQLCommand);
            Bukkit.getLogger().info("AddonsSheep2(SQLite) > Request all are set!");
            return true;
        }

        catch(SQLException e)
        { e.printStackTrace(); return false; }
    }

    public boolean runSQLCommand(String command)
    {
        try
        { return databaseConnection.createStatement().execute(command); }

        catch(SQLException e)
        { e.printStackTrace(); return false; }
    }

    public Connection getDatabaseConnection()
    { return databaseConnection; }

    public boolean close()
    {
        try { databaseConnection.close(); return true; }
        catch (SQLException e) { e.printStackTrace(); return false; }
    }
}

